/**
 * 判断是否为外链
 */
export function isExternal(path: string): boolean {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * 判断是否为数字
 */
export function isNumber(value: string): boolean {
  const reg = /^[0-9]*$/
  return reg.test(value)
}

/**
 * 判断是否为小写字母
 */
export function isLowerCase(str: string): boolean {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * 判断是否为大写字母
 */
export function isUpperCase(str: string): boolean {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * 判断是否为字母
 */
export function isAlphabets(str: string): boolean {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * 判断是否为字符串
 */
export function isString(obj: any): boolean {
  return Object.prototype.toString.call(obj) === '[object String]'
}

/**
 * 判断是否为数组
 */
export function isArray(arg: any): boolean {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * 验证邮箱
 */
export function isEmail(email: string): boolean {
  const reg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
  return reg.test(email)
}

/**
 * 验证手机号
 */
export function isMobile(mobile: string): boolean {
  const reg = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/
  return reg.test(mobile)
}

/**
 * 验证URL
 */
export function isURL(url: string): boolean {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * 验证IP地址
 */
export function isIP(ip: string): boolean {
  const reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
  return reg.test(ip)
}

/**
 * 验证身份证号
 */
export function isIdCard(idCard: string): boolean {
  const reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
  return reg.test(idCard)
}

/**
 * 验证密码强度
 */
export function isStrongPassword(password: string): boolean {
  // 至少8位，包含大小写字母、数字和特殊字符
  const reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
  return reg.test(password)
}

/**
 * 验证用户名
 */
export function isValidUsername(username: string): boolean {
  // 4-16位，可包含字母、数字、下划线，以字母开头
  const reg = /^[a-zA-Z][a-zA-Z0-9_]{3,15}$/
  return reg.test(username)
}

/**
 * 验证中文
 */
export function isChinese(str: string): boolean {
  const reg = /^[\u4e00-\u9fa5]+$/
  return reg.test(str)
}

/**
 * 验证日期格式 YYYY-MM-DD
 */
export function isDate(dateString: string): boolean {
  const reg = /^\d{4}-\d{2}-\d{2}$/
  return reg.test(dateString)
}

/**
 * 验证时间格式 HH:MM:SS
 */
export function isTime(timeString: string): boolean {
  const reg = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/
  return reg.test(timeString)
}

/**
 * 验证日期时间格式 YYYY-MM-DD HH:MM:SS
 */
export function isDateTime(dateTimeString: string): boolean {
  const reg = /^\d{4}-\d{2}-\d{2} ([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/
  return reg.test(dateTimeString)
}
